package com.qfedu.yanglao20admin.advertising.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.advertising.AdvertisingContent;
import com.qfedu.yanglao20.entity.advertising.AdvertisingType;
import com.qfedu.yanglao20admin.advertising.dao.AdvertisingContentDao;
import com.qfedu.yanglao20admin.advertising.param.AdvertisingContenParam;
import com.qfedu.yanglao20admin.advertising.service.AdvertisingContenService;
import com.qfedu.yanglao20admin.advertising.vo.AdvertisingContentVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 广告内容信息业务层实现类
 */
@Service
public class AdvertisingContenServiceImpl implements AdvertisingContenService {

    @Resource
    private AdvertisingContentDao contentDao;

    @Override
    public void deleteById(Integer id) {
        contentDao.deleteById(id);
    }

    @Override
    public Page<AdvertisingContentVO> pageAdvertisingContent(AdvertisingContenParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        //分页查询
        Page<AdvertisingContentVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<AdvertisingContentVO> advertisingContentVOS = contentDao.selectList(param);

        //页码
        page.setCurrent(param.getPageNum());
        //每页查询的记录数
        page.setSize(param.getPageSize());
        //总记录数
        Long aLong = contentDao.selectCount(null);
        page.setTotal(aLong);
        page.setRecords(advertisingContentVOS);
        return page;
    }

    @Override
    public void add(AdvertisingContentVO advertisingContentVO) {

        if (advertisingContentVO.getTitle() == null || advertisingContentVO.getTitle().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写广告标题");
        }

        //通过前端发送的标题名字查询出来对应的id
        AdvertisingType advertisingType = contentDao.selectByTitle(advertisingContentVO.getTypeName());
        Integer id = advertisingType.getId();

        String title = advertisingContentVO.getTitle();
        String imgUrl = advertisingContentVO.getImgUrl();
        String sort = advertisingContentVO.getSort();
        Integer state = advertisingContentVO.getState();

        //把相对应的数据()装到一个新实体类对象中
        AdvertisingContent advertisingContent1 = new AdvertisingContent();
        advertisingContent1.setTitle(title);
        advertisingContent1.setImgUrl(imgUrl);
        advertisingContent1.setTypeId(id);
        advertisingContent1.setSort(sort);
        advertisingContent1.setState(state);
        //执行增加的sql语句
        contentDao.insert(advertisingContent1);
    }

    @Override
    public AdvertisingContentVO queryAdverisingContent(Integer id) {
        return contentDao.selectById(id);
    }

    @Override
    public void update(AdvertisingContentVO advertisingContentVO) {
        if (advertisingContentVO.getTitle() == null || advertisingContentVO.getTitle().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写广告标题");
        }

        //通过前端发送的标题名字查询出来对应的id
        AdvertisingType advertisingType = contentDao.selectByTitle(advertisingContentVO.getTypeName());
        Integer typeId = advertisingType.getId();

        Integer id = advertisingContentVO.getId();
        String title = advertisingContentVO.getTitle();
        String imgUrl = advertisingContentVO.getImgUrl();
        String sort = advertisingContentVO.getSort();
        Integer state = advertisingContentVO.getState();

        //把相对应的数据()装到一个新实体类对象中
        AdvertisingContent advertisingContent1 = new AdvertisingContent();
        advertisingContent1.setId(id);
        advertisingContent1.setTitle(title);
        advertisingContent1.setImgUrl(imgUrl);
        advertisingContent1.setTypeId(typeId);
        advertisingContent1.setSort(sort);
        advertisingContent1.setState(state);
        //执行更新的sql语句
        contentDao.updateById(advertisingContent1);
    }
}
